在生活中,我們常常面臨選擇,
今天午餐吃什麼好呢?飯還是麵呢?
明天要去哪裡玩?山上還是海邊呢?
那這些選擇的邏輯結構在C#中我們稱為選取範圍語句

if....else...if ( 條件 )
{
..............
}
else
{
...............
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告天氣
            string weather = "下雨";
            Console.WriteLine("今天天氣 : " + weather);
            //使用if陳述句來做判斷
            if(weather == "下雨")
            {
                Console.WriteLine("待在家");
            }
            else
            {
                Console.WriteLine("出去玩");
            }
            //把天氣改成晴天,再做一次判斷試試看
            weather = "晴天";
            Console.WriteLine("今天天氣 : " + weather);
            //使用if陳述句來做判斷
            if (weather == "下雨")
            {
                Console.WriteLine("待在家");
            }
            else
            {
                Console.WriteLine("出去玩");
            }
            Console.ReadKey();
        }
    }
}
結果:
今天天氣 : 下雨
待在家
今天天氣 : 晴天
出去玩

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入年齡");
            //宣告年齡,並將輸入的值賦予給他
            int age = Convert.ToInt32(Console.ReadLine());
            //判斷年齡
            if(age < 18)
            {
                Console.WriteLine(age + " 歲是未成年,不可進入!");
            }
            else
            {
                Console.WriteLine(age + " 歲已成年,歡迎光臨!");
            }
            Console.ReadKey();
        }
    }
 }
輸入:
48
結果:
請輸入年齡
48
48 歲已成年,歡迎光臨!
輸入:
15
結果:
請輸入年齡
15
15 歲是未成年,不可進入!

else if做使用using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入年齡");
            //宣告年齡,並將輸入的值賦予給他
            int age = Convert.ToInt32(Console.ReadLine());
            //判斷年齡
            if (age < 18)
            {
                Console.WriteLine(age + " 歲是未成年,任何駕照都不能考!");
            }
            else if (age < 20)
            {
                Console.WriteLine(age + " 歲,可以考機車駕照,但不能考重機駕照!");
            }
            else
            {
                Console.WriteLine(age + " 歲,任何駕照都可以考!");
            }
           Console.ReadKey();
        }
    }
  }
輸入:
15
結果:
請輸入年齡
15
15 歲是未成年,任何駕照都不能考!
輸入:
19
結果:
請輸入年齡
19
19 歲,可以考機車駕照,但不能考重機駕照!
輸入:
48
結果:
請輸入年齡
48
48 歲,任何駕照都可以考!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //今天
            Console.WriteLine("今天星期幾?");
            string today = Console.ReadLine();
            //天氣
            Console.WriteLine("今天天氣?");
            string weather = Console.ReadLine();
            //先判斷今天日是不是假日
            if (today == "星期六" || today == "星期日")
            {
                if(weather == "晴天")
                {
                    Console.WriteLine("今天" + today + "," + weather + ",來去海邊玩");
                }
                else
                {
                    Console.WriteLine("今天" + today + "," + weather + ",來去爬山");
                }
            }
            else
            {
                Console.WriteLine("今天" + today + ",所以要上課");
            }
            Console.ReadKey();
        }
    }
 }
輸入:
星期六
晴天
結果:
今天星期幾?
星期六
今天天氣?
晴天
今天星期六,晴天,來去海邊玩
輸入:
星期日
陰天
結果:
今天星期幾?
星期日
今天天氣?
陰天
今天星期日,陰天,來去爬山
輸入:
星期一
陰天
結果:
今天星期幾?
星期一
今天天氣?
陰天
今天星期一,所以要上課